function localStorage() {
  return window.localStorage;
}

function get(key) {
  try {
    return JSON.parse(localStorage().getItem(key));
  } catch (e) {
    return {};
  }
}

function set(key, data) {
  return localStorage().setItem(key, JSON.stringify(data));
}

function saveLanguage(lang) {
  return localStorage().setItem("lang", lang);
}

function saveUserInfo(data) {
  set("userInfo", data);
}

function getUserInfo() {
  return get("userInfo");
}

function all() {
  const data = {};
  for (var i = localStorage().length - 1; i >= 0; i--) {
    var key = localStorage().key(i);
    data[key] = get(key);
  }

  return data;
}

function remove(key) {
  return localStorage().removeItem(key);
}

function clearAll() {
  return localStorage().clear();
}

function has(key) {
  return localStorage().getItem(key) !== null;
}

export default {
  get,
  set,
  all,
  remove,
  clearAll,
  has,
  saveUserInfo,
  getUserInfo
};
